#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2014, Joyent, Inc.
#

# coal-vmware-setup: sets up VMWare Fusion (on a Mac) so you can use
# CoaL (Cloud on a Laptop) with it.

if [[ -n "$TRACE" ]]; then
    export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi
set -o errexit
set -o pipefail



if [[ "$(uname -s)" != "Darwin" ]]; then
    echo "error: This script is written for a Mac" >&2
    exit 1
fi

if [[ $UID -ne 0 ]]; then
    echo "error: This script must be run as root. Run again using:" >&2
    echo "    sudo $0" >&2
    exit 1
fi

BIN_DIR=/Library/Application\ Support/VMware\ Fusion
SETTINGS_DIR=/Library/Application\ Support/VMware\ Fusion
NET_TMP=/tmp/coal.networking.$$
LOCATIONS_TMP=/tmp/coal.locations.$$

# Detect if Fusion 4 is installed
if [[ -e "/Applications/VMware Fusion.app/Contents/Library/services.sh" ]]; then
    BIN_DIR="/Applications/VMware Fusion.app/Contents/Library"
    SETTINGS_DIR="/Library/Preferences/VMware Fusion"
    FUSION4=true
fi

# Detect if Fusion 7 or Fusion 8 is installed
if [[ -e "/Applications/VMware Fusion.app/Contents/Library/vmrun" ]]; then
    BIN_DIR="/Applications/VMware Fusion.app/Contents/Library"
    SETTINGS_DIR="/Library/Preferences/VMware Fusion"
    FUSION7=true
fi

## Networks ##
# The admin "host-only" network - vmnet1 (only between the Mac and the headnode)
ADMIN_NETWORK=10.99.99.0
ADMIN_NETMASK=255.255.255.0
ADMIN_MAC_IP=$(echo ${ADMIN_NETWORK} | sed 's/\.[0-9]*$/.254/')

# The external NAT network - vmnet8 (can reach the internet)
# Note: There isn't currently an "external_network" var in config.coal to use.
EXTERNAL_NETWORK=10.88.88.0
EXTERNAL_NETMASK=255.255.255.0
EXTERNAL_MAC_IP=10.88.88.1

echo "Admin network:    network=\"${ADMIN_NETWORK}\", mac ip=\"${ADMIN_MAC_IP}\", netmask=\"${ADMIN_NETMASK}\""
echo "External network: network=\"${EXTERNAL_NETWORK}\", mac ip=\"${EXTERNAL_MAC_IP}\", netmask=\"${EXTERNAL_NETMASK}\""

cd "$SETTINGS_DIR"

VM_LIST=$("$BIN_DIR/vmrun" list)
if [[ "$VM_LIST" != "Total running VMs: 0" ]]; then
    echo You have Virtual Machines running.  Please shut them down before continuing.
    exit 1
fi

echo "Setup VMWare networking: admin network $ADMIN_NETWORK, external network $EXTERNAL_NETWORK"
cat networking | (grep -ve 'VNET_[18]' || true) > $NET_TMP

cat >> $NET_TMP <<__NETWORKING__
answer VNET_1_DHCP no
answer VNET_1_HOSTONLY_NETMASK $ADMIN_NETMASK
answer VNET_1_HOSTONLY_SUBNET $ADMIN_NETWORK
answer VNET_1_NAT no
answer VNET_1_VIRTUAL_ADAPTER yes
answer VNET_1_VIRTUAL_ADAPTER_ADDR $ADMIN_MAC_IP
answer VNET_8_DHCP yes
answer VNET_8_DHCP_CFG_HASH 889DD78A4E0F3E1F68AB5418242607E2B4A060B7
answer VNET_8_HOSTONLY_NETMASK $EXTERNAL_NETMASK
answer VNET_8_HOSTONLY_SUBNET $EXTERNAL_NETWORK
answer VNET_8_NAT yes
answer VNET_8_VIRTUAL_ADAPTER yes
answer VNET_8_VIRTUAL_ADAPTER_ADDR $EXTERNAL_MAC_IP
__NETWORKING__

if [[ -e locations ]]; then
    cat locations | (grep -ve 'VNET_[18]' || true) > $LOCATIONS_TMP
cat >> $LOCATIONS_TMP <<__LOCATIONS__
remove_answer VNET_1_HOSTONLY_HOSTADDR
remove_answer VNET_1_HOSTONLY_NETMASK
answer VNET_1_HOSTONLY_HOSTADDR $ADMIN_MAC_IP
answer VNET_1_HOSTONLY_NETMASK $ADMIN_NETMASK
remove_answer VNET_1_DHCP
answer VNET_1_DHCP no
remove_answer VNET_8_HOSTONLY_HOSTADDR
remove_answer VNET_8_HOSTONLY_NETMASK
answer VNET_8_HOSTONLY_HOSTADDR $EXTERNAL_MAC_IP
answer VNET_8_HOSTONLY_NETMASK $EXTERNAL_NETMASK
remove_answer VNET_8_NAT
answer VNET_8_NAT yes
remove_answer VNET_8_DHCP
answer VNET_8_DHCP yes
__LOCATIONS__

echo Changing VMware locations settings...

cp locations locations.pre_coal

echo Old locations settings backed up to the following files:
echo     $SETTINGS_DIR/locations.pre_coal

mv $LOCATIONS_TMP locations
fi

echo Changing VMware networking settings...

cp networking networking.pre_coal

echo Old networking settings backed up to the following files:
echo     $SETTINGS_DIR/networking.pre_coal

mv $NET_TMP networking

echo Restarting VMware services...
if [[ -n "$FUSION4" ]]; then
    "$BIN_DIR/services.sh" --stop
    "$BIN_DIR/services.sh" --start
elif [[ -n "$FUSION7" ]]; then
    echo "Restart Fusion after this script finishes!"
else
    "$BIN_DIR/boot.sh" --restart
fi

# If we're running before VMWare is run for the first time, the previous step
# will have created a preference file as root, so fix it:
chown -R $SUDO_UID:$SUDO_GID "/Users/$SUDO_USER/Library/Preferences/VMWare Fusion"

echo Done!

